#!/usr/bin/env python
# coding=utf-8
"""
# @Created on : 2016/05/04 9:57
# @Author     : ldf (ldf@eehello.com)
# @Link       : http://www.eehello.com
# @Version    : 
# 本程序在python2.7下测试通过
"""

'''
from threading import Thread
from Queue import Queue
import time


class shengchanzhe(Thread):

    def __init__(self,name,queue):
        self.__name = name
        self.__queue = queue
        super(shengchanzhe,self).__init__()

    def run(self):
        while 1:
            if self.__queue.full():
                time.sleep(3)
            else:
                self.__queue.put('baozi')
                time.sleep(2)
                print '%s 生产了一个包子' % (self.__name,)
        #Thread.run(self)


class xiaofeizhe(Thread):

    def __init__(self,name,queue):
        self.__name = name
        self.__queue = queue
        super(xiaofeizhe,self).__init__()

    def run(self):
        while 1:
            if self.__queue.empty():
                time.sleep(1)
            else:
                try:
                    self.__queue.get_nowait()
                    print '%s 消费了一个包子' % (self.__name,)
                except Exception:
                    print '没有包子了'
        #Thread.run(self)

que = Queue(maxsize=100)

laogou1 = shengchanzhe('老狗1',que)
laogou1.start()
laogou2 = shengchanzhe('老狗2',que)
laogou2.start()
laogou3 = shengchanzhe('老狗3',que)
laogou3.start()

for item in range(20):
    name = 'chentao%d' % item
    temp = xiaofeizhe(name,que)
    temp.start()
'''

'''
print que.qsize()
que.put('1')
que.put('2')
print que.qsize()
print 'get:',que.get()
print 'get:',que.get()
print que.qsize()
print 'empty:',que.empty()
'''
import threading
import time


num = 0
a = []
def run(n):
    time.sleep(1)
    global num
    samp.acquire()
    time.sleep(0.001)
    num += 1
    print '%s' % num
    samp.release()


samp = threading.BoundedSemaphore(4)

for i in range(200):
    t = threading.Thread(target=run,args=(i,))
    t.start()
